package wodegua.d25_recursion;


public class RecursionDemo4 {
    private static int sum;
    private static int pg;
    private static int kp;
    public static void main(String[] args) {
        // 啤酒问题:啤酒2元一瓶，4个盖子可以换一瓶，2个空可以换一瓶，请问10元可以喝多少瓶?
//        f(5,5,5);
        f1(10);
        System.out.println("pg="+pg+";kp="+kp+";sum="+sum);


        /**
         * 盖子-空瓶-兑换-盖子-空瓶=总数
         * 0-0-0-5-5=5
         * 1-2-3-4-3=8
         * 1-1-2-2-3=10
         * 0-1-1-3-1=11
         */
    }

    private static void f(int pg, int kp, int sum){
//        System.out.println("pg="+pg+";kp="+kp+";sum="+sum);
        if (pg < 4 && kp < 2) {
            System.out.println("这么多瓶" + sum);
            return;
        }
        int pgNumber = pg/4;
        int kpNumber = kp/2;
        int number = pgNumber + kpNumber;
        pg = pg - pgNumber * 4 + number;
        kp = kp - kpNumber * 2 + number;
        sum += number;
        f(pg, kp, sum);
    }

    private static void f1(int money){
        if (money < 2){
            return;
        }
        int buyNUmber = money / 2;
        sum += buyNUmber;

        int pgCount = pg + buyNUmber;
        int kpCount = kp + buyNUmber;

        int pgMoney = pgCount/4 * 2;
        int kpMoney = kpCount/2 * 2;

        pg = pgCount%4;
        kp = kpCount%2;

        int totalMoney = money%2 + pgMoney + kpMoney;
        f1(totalMoney);
    }
}
